package com.lambo.maven.core.utils.superHttp.entity;

import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.util.EntityUtils;

/**
 * HttpResult
 */
public class HttpResult {

 
    private HttpEntity resEntity;
    private HttpRequestBase httpPost;
    private CloseableHttpResponse httpResponse;
    private Header[] headers;
    
    public HttpResult(HttpEntity resEntity, HttpRequestBase httpPost, CloseableHttpResponse httpResponse) {
        this.resEntity = resEntity;
        this.httpPost = httpPost;
        this.httpResponse = httpResponse;
        this.headers = httpResponse.getAllHeaders();
	}

	public HttpEntity getResEntity() {
        return this.resEntity;
    }

    public void setResEntity(HttpEntity resEntity) {
        this.resEntity = resEntity;
    }

    public HttpRequestBase getHttpPost() {
        return this.httpPost;
    }

    public void setHttpPost(HttpRequestBase httpPost) {
        this.httpPost = httpPost;
    }

    public byte[] toByteArray() throws IOException {
        byte[] res = EntityUtils.toByteArray(resEntity);
        return res;
    }

	public String toString(String encoding) throws ParseException, IOException {
        String res = EntityUtils.toString(resEntity, encoding);
        return res;
	}

    public Header[] getHeaders() {
        return headers;
    }

    public Map<String, String> getHeaderMap() {
        Map<String, String> headerMap = new LinkedHashMap<String, String>();
        for(Header header : headers) {
            headerMap.put(header.getName(), header.getValue());
        }
        return headerMap;
    }
}